package com.example.interview;

import com.example.leetcode.tree.common.TreeNode;
import com.example.leetcode.tree.common.TreeUtils;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;

public class TreeTraverse {
    public static void main(String[] args) {
        Integer[] nums = new Integer[10];
        Random random = new Random();
        for (int i = 0; i < nums.length; i++) {
            nums[i] = random.nextInt(20);
        }
        TreeNode root = TreeUtils.generateLeetcodeTree(nums);

        preTraverse(root);
        midTraverse(root);
        postTraverse(root);
        bfs(root);
        dfsByStack(root);

    }

    /**
     * 用栈实现的深度优先遍历，即前序遍历
     *
     * @param root
     */
    private static void dfsByStack(TreeNode root) {
        System.out.println("用栈实现的深度优先遍历");
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            System.out.println(root.val);
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (stack.size() > 0) {
            TreeNode node = stack.pop();
            System.out.println(node.val);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
    }

    /**
     * 广度优先遍历
     *
     * @param root
     */
    private static void bfs(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            System.out.println(root.val);
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (queue.size() > 0) {
            TreeNode node = queue.poll();
            System.out.println(node.val);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
    }

    /**
     * 后序遍历
     *
     * @param root
     */
    private static void postTraverse(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            System.out.println(root.val);
            return;
        }
        preTraverse(root.left);
        preTraverse(root.right);
        System.out.println(root.val);
    }

    /**
     * 中序遍历
     *
     * @param root
     */
    private static void midTraverse(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            System.out.println(root.val);
            return;
        }
        preTraverse(root.left);
        System.out.println(root.val);
        preTraverse(root.right);
    }

    /**
     * 前序遍历
     *
     * @param root
     */
    private static void preTraverse(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            System.out.println(root.val);
            return;
        }
        System.out.println(root.val);
        preTraverse(root.left);
        preTraverse(root.right);
    }
}
